package com.haitaoit.chengquan.module.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.home.adapter.SearchAdapter;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.Timer;
import java.util.TimerTask;

public class SearchActivity extends BaseActivity {
    @ViewInject(R.id.history_search)
    MyListView history_search;
    @ViewInject(R.id.search_et)
    EditText   search_et;
    //    @ViewInject(R.id.cancel)
    //    TextView cancel;


    private SearchAdapter adapter;
    private String[] Search_History = null;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ViewUtils.inject(this);

        //设置获取焦点
        search_et.setFocusable(true);
        search_et.setFocusableInTouchMode(true);
        search_et.requestFocus();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            }
        }, 200);//

        adapter = new SearchAdapter(this);
        history_search.setAdapter(adapter);
        type = getIntent().getStringExtra("type");
        history_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                if ("forum".equals(type)) {// 帖子论坛
                    Intent i = new Intent();
                    i.putExtra("search_title", Search_History[arg2]);
                    setResult(1, i);
                    finish();
                } else {
                    Intent intent = new Intent(SearchActivity.this, SearchListActivity.class);
                    intent.putExtra("search_title", Search_History[arg2]);
                    startActivity(intent);
                }
            }
        });

        if ("forum".equals(type)) {

            // 帖子论坛
            search_et.setText(getIntent().getStringExtra("search_title"));
        }

        search_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    startSearch();
                    return true;
                }
                return false;
            }
        });
    }
    @Override
    public void onResume() {
        String search = PreferenceUtils.getPrefString("Search_History", "");
        if (search.length() != 0) {
            Search_History = search.split(",");
        } else {
            Search_History = null;
        }
        adapter.setSearch(Search_History);
        adapter.notifyDataSetChanged();
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    public void hideKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(search_et, InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(search_et.getWindowToken(), 0); //强制隐藏键盘
    }

    @OnClick({R.id.left, R.id.search_img, R.id.cancel, R.id.search_clean})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                hideKeyBoard();
                finish();
                break;
            case R.id.search_img:// 搜索
                startSearch();
                break;
            case R.id.cancel:
                search_et.setText("");
                hideKeyBoard();
                finish();
                break;
            case R.id.search_clean:
                PreferenceUtils.setPrefString("Search_History", "");
                Search_History = null;
                adapter.setSearch(Search_History);
                adapter.notifyDataSetChanged();
                ToastUtils.showToast("清除成功！");
                break;
        }
        super.onClick(v);
    }

    private void startSearch() {
        if (!search_et.getText().toString().trim().equals("")) {
            String search = PreferenceUtils.getPrefString("Search_History", "");
            String[] s = search.split(",");
            boolean bool = true;

            for (int i = 0; i < s.length; i++) {
                if (s[i].equals(search_et.getText().toString())) {
                    bool = false;
                }
            }

            if (bool) {
                search = search_et.getText().toString() + "," + search;
                PreferenceUtils.setPrefString("Search_History", search);
            }
            if ("forum".equals(type)) {// 帖子论坛
                Intent i = new Intent();
                i.putExtra("search_title", search_et.getText().toString());
                setResult(1, i);
                finish();
            } else {
                Intent intent = new Intent(SearchActivity.this, SearchListActivity.class);
                intent.putExtra("search_title", search_et.getText().toString());
                startActivity(intent);
            }
            search_et.setText("");
        } else {
            ToastUtils.showToast("亲，请输入搜索内容！");
        }
    }
}
